<?php

namespace Home\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Home\Model\Photo;
use Zend\Db\Sql\Select;
use Zend\Paginator\Paginator;
use Zend\Paginator\Adapter\Iterator as paginatorIterator;
use Zend\Session\Container;

class PhotoController extends AbstractActionController {

    
	public $menuTable;
	public $tintucTable;
	public $onlineTable;
	public $lienketTable;
	public $counterTable;
	public $visitTable;
	public $galleryTable;
	protected $photoTable;
	public $thongtinTable;

	public function counter(){
		$sid = session_id();
		$CurrentTime = time();
		$TimeOut = $CurrentTime - 300;
		$this->getOnlineTable()->check_exists($sid,$CurrentTime,$TimeOut);
		$select = new Select();
		$online = $this->getOnlineTable()->fetchAll()->count();
		$this->getEvent()->getViewModel()->online = $online;
		////
		$today = date('d');
		$thang = date('m');
		$nam = date('Y');
		$ip = $this->getRequest()->getServer('REMOTE_ADDR');
		if($this->getVisitTable()->check_date($today,$thang,$nam))
		{
			if($this->getVisitTable()->check_ip($ip,$today,$thang,$nam))
			{
				if($sid!=$this->getVisitTable()->check_ip($ip,$today,$thang,$nam)->session)
				{
					$this->getVisitTable()->updateVisit($ip,$sid,$today,$thang,$nam);
					$this->getCounterTable()->updateLuotxem($today,$thang,$nam);
				}
			}
			else
			{
				$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
				$this->getCounterTable()->updateLuongtruycap($today,$thang,$nam);
			}
		}
		else
		{
			$this->getVisitTable()->deleteVisit();
			$this->getVisitTable()->insertVisit($ip,$sid,$today,$thang,$nam);
			$this->getCounterTable()->insertCounter($today,$thang,$nam);
		}
		//$this->getCounterTable()->check_exists($today,$thang,$nam);
		
		$select = new Select();
		$tongtruycap = $this->getCounterTable()->sumCounter();
		
		$this->getEvent()->getViewModel()->tongtruycap = $tongtruycap->luongtruycap;
		/////// \Zend\Debug\Debug::dump($tongtruycap->luongtruycap);
		
		$select = new Select();
		$thongtin = $this->getThongtinTable()->fetchAll();
		
		$this->getEvent()->getViewModel()->banner = $thongtin->banner;
		$this->getEvent()->getViewModel()->footer = $thongtin->footer;
		$this->getEvent()->getViewModel()->yahoo = $thongtin->yahoo;
		$this->getEvent()->getViewModel()->skype = $thongtin->skype;
		
		
		$module = "home";
		$controller = $this->params('controller');
		$action = $this->params('action');
		
		$sessionModule = new Container('module');
		$sessionController = new Container('controller');
		$sessionAction = new Container('action');
		$sessionModule->lastModule = $module;
		$sessionController->lastController = $controller;
		$sessionAction->lastAction = $action;
		
		$select = new Select();
		
		//// domain
		$domain = "/truonghoc";
		$this->getEvent()->getViewModel()->domain = $domain;
		///////
		$this->getEvent()->getViewModel()->chuyenmuc = "photo";

        $danhmuc = $this->getMenuTable()->fetchAllDanhmuc();
        $this->getEvent()->getViewModel()->danhmuc = $danhmuc;

		$canbiet = $this->getMenuTable()->fetchAllCanbiet();
        $this->getEvent()->getViewModel()->canbiet = $canbiet;
		
		$menuchinh = $this->getMenuTable()->fetchAllMenuchinh();
        $this->getEvent()->getViewModel()->menuchinh = $menuchinh;
		$this->getEvent()->getViewModel()->menuchinh1 = $menuchinh;
		
		$select = new Select();
		$noibat = $this->getTintucTable()->fetchAll($select->where(array('tinnoibat' => 'Nổi bật','trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->noibat = $noibat;
		
		/////
		$select = new Select();
		$moinhat = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('id desc')->limit(5));
		$this->getEvent()->getViewModel()->moinhat = $moinhat;
		
		/////
		/////
		$select = new Select();
		$quantam = $this->getTintucTable()->fetchAll($select->where(array('trangthai' => 'Hiển thị'))->order('soluotxem desc')->limit(5));
		$this->getEvent()->getViewModel()->quantam = $quantam;
		
		/////
		/////
		$select = new Select();
		$lienketwebsite = $this->getLienketTable()->fetchAll();
		$this->getEvent()->getViewModel()->lienketwebsite = $lienketwebsite;
	}
    public function indexAction() {
		
		////
		$this->counter();
		
		
		$select = new Select();
		$gallery = $this->getGalleryTable()->fetchAll();
		

       	return new ViewModel(array(
					
					'gallery' => $gallery,
                ));
        
		
    }
	public function detailsAction() {
		
		////
		$this->counter();
		
		$id_gallery = $this->params()->fromRoute('id');
		$select = new Select();
		$photo = $this->getPhotoTable()->fetchAll($select->where(array('gallery' => $id_gallery)));
		
		$select = new Select();
		$tengallery = $this->getPhotoTable()->getName($select->where(array('gallery' => $id_gallery)));

       	return new ViewModel(array(
					
					'photo' => $photo,
					'tengallery' => $tengallery,
                ));
        
		
    }

    

    
	public function getMenuTable() {
        if (!$this->menuTable) {
            $sm = $this->getServiceLocator();
            $this->menuTable = $sm->get('Home\Model\MenuTable');
        }
        return $this->menuTable;
    }
	public function getTintucTable() {
        if (!$this->tintucTable) {
            $sm = $this->getServiceLocator();
            $this->tintucTable = $sm->get('Home\Model\TintucTable');
        }
        return $this->tintucTable;
    }
	public function getOnlineTable() {
        if (!$this->onlineTable) {
            $sm = $this->getServiceLocator();
            $this->onlineTable = $sm->get('Home\Model\OnlineTable');
        }
        return $this->onlineTable;
    }
	public function getLienketTable() {
        if (!$this->lienketTable) {
            $sm = $this->getServiceLocator();
            $this->lienketTable = $sm->get('Home\Model\LienketTable');
        }
        return $this->lienketTable;
    }
	public function getCounterTable() {
        if (!$this->counterTable) {
            $sm = $this->getServiceLocator();
            $this->counterTable = $sm->get('Home\Model\CounterTable');
        }
        return $this->counterTable;
    }
	public function getVisitTable() {
        if (!$this->visitTable) {
            $sm = $this->getServiceLocator();
            $this->visitTable = $sm->get('Home\Model\VisitTable');
        }
        return $this->visitTable;
    }
	public function getPhotoTable() {
        if (!$this->photoTable) {
            $sm = $this->getServiceLocator();
            $this->photoTable = $sm->get('Home\Model\PhotoTable');
        }
        return $this->photoTable;
    }
	public function getGalleryTable() {
        if (!$this->galleryTable) {
            $sm = $this->getServiceLocator();
            $this->galleryTable = $sm->get('Home\Model\GalleryTable');
        }
        return $this->galleryTable;
    }
	public function getThongtinTable() {
        if (!$this->thongtinTable) {
            $sm = $this->getServiceLocator();
            $this->thongtinTable = $sm->get('Home\Model\ThongtinTable');
        }
        return $this->thongtinTable;
    }

}
